package com.moson.girl.service;

import com.moson.girl.Repository.GirlRepository;
import com.moson.girl.domain.Girl;
import com.moson.girl.enums.ResultEnum;
import com.moson.girl.exception.GirlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * @Author: MoXingjian
 * @Description:
 * @Date: Created in 18:27 2018/1/7
 * @Modified By:
 */
@Service
public class GirlService {

    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void insertTwo(){
        Girl girlA = new Girl();
        girlA.setAge(18);
        girlA.setCupSize("A");
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setAge(19);
        girlB.setCupSize("CCC");
        girlRepository.save(girlB);
    }

    /**
     * 获取年龄
     * @param id
     * @throws Exception
     */
    public void getAge(Integer id) throws Exception{
        Girl girl = girlRepository.getOne(id);
        Integer age = girl.getAge();
        if (age < 10){
            throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
        } else if (age >= 10 && age < 16) {
            throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
        }
    }

    /**
     * 通过ID查询一个女生
     * @param id 主键
     * @return 女生
     */
    public Girl findOne(Integer id){
        return girlRepository.findOne(id);
    }
}
